package com.draven.mall.controller;

import com.draven.mall.common.ApiResponse;
import com.draven.mall.entity.MerchantApplication;
import com.draven.mall.service.MerchantAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商家审核控制器
 */
@RestController
@RequestMapping("/api/admin/merchant")
public class MerchantAuditController {

    @Autowired
    private MerchantAuditService merchantAuditService;

    /**
     * 获取待审核商家列表
     */
    @GetMapping("/pending")
    public ApiResponse getPendingMerchants() {
        List<MerchantApplication> merchants = merchantAuditService.getPendingMerchants();
        return ApiResponse.success(merchants);
    }

    /**
     * 获取商家详情
     */
    @GetMapping("/{id}")
    public ApiResponse getMerchantDetail(@PathVariable Long id) {
        MerchantApplication merchant = merchantAuditService.getMerchantById(id);
        if (merchant != null) {
            return ApiResponse.success(merchant);
        } else {
            return ApiResponse.error("商家信息不存在");
        }
    }

    /**
     * 审核商家
     */
    @PostMapping("/audit")
    public ApiResponse auditMerchant(@RequestBody Map<String, Object> params) {
        Long id = Long.parseLong(params.get("id").toString());
        Integer status = Integer.parseInt(params.get("status").toString());
        String reason = (String) params.get("reason");
        
        boolean success = merchantAuditService.auditMerchant(id, status, reason);
        
        if (success) {
            return ApiResponse.success("审核操作成功");
        } else {
            return ApiResponse.error("审核操作失败");
        }
    }

    /**
     * 获取商家列表
     */
    @GetMapping("/list")
    public ApiResponse getMerchantList(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size) {
        
        Map<String, Object> result = merchantAuditService.getMerchantList(keyword, page, size);
        return ApiResponse.success(result);
    }
} 